﻿using System;
using System.Net;
#if SILVERLIGHT
using AbstraX.ClientInterfaces;
#else
using AbstraX.ServerInterfaces;
#endif
using System.Reflection;
using AbstraX.XPathBuilder;
using System.Linq;
using System.Xml.Schema;
using System.Diagnostics;
using System.ServiceModel.DomainServices.Server;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using AssemblyProvider.Web.Entities;
using ControlProviders.ProviderInterfaces;
using AbstraX;
using AbstraX.Templates;
using AbstraX.AssemblyInterfaces;
using AbstraX.Contracts;
using MvvmTreeView;

namespace SLControlProvider.Web.Entities
{
    [DataContract, DebuggerDisplay("{ DebugInfo }"), ClientCodeGeneration(typeof(AbstraXClientInterfaceGenerator))]
    public class SLFieldAttribute : IControlAttribute
    {
        protected IFieldAttribute field;
        protected IBase parent;
        protected IProviderEntityService providerEntityService;

        public SLFieldAttribute()
        {
        }

        public SLFieldAttribute(IFieldAttribute field, IBase parent)
        {
            this.field = field;
            this.parent = parent;

            providerEntityService = ((IBase)parent).ProviderEntityService;
        }

        public IAbstraXExtension LoadExtension()
        {
            return null;
        }

        [DataMember]
        public float ChildOrdinal
        {
            get
            {
                return field.ChildOrdinal;
            }
        }

        [DataMember]
        public string DebugInfo
        {
            get
            {
                return this.GetDebugInfo();
            }
        }

        [Include, Association("Parent_BaseType", "ID", "ParentID")]
        public ScalarType DataType
        {
            get
            {
                return new ScalarType(field.DataType, this);
            }
        }

        [DataMember, Key]
        public string ID
        {
            get 
            {
                return field.ID;
            }
        }

        [DataMember]
        public string ParentID
        {
            get
            {
                return field.ParentID;
            }
        }

        [DataMember]
        public string Name
        {
            get 
            {
                return field.Name;
            }
        }

        [DataMember]
        public string DesignComments
        {
            get
            {
                return field.DesignComments;
            }
        }

        [DataMember]
        public string DocumentationSummary
        {
            get
            {
                return field.DocumentationSummary;
            }
        }

        [DataMember]
        public string Documentation
        {
            get
            {
                return field.Documentation;
            }
        }

        [DataMember]
        public bool HasDocumentation
        {
            get
            {
                return field.HasDocumentation;
            }
        }

        [DataMember]
        public string ImageURL
        {
            get
            {
                return field.ImageURL;
            }
        }

        [DataMember]
        public IBase Parent
        {
            get
            {
                return field.Parent;
            }
        }

        [DataMember]
        public string FolderKeyPair
        {
            get
            {
                return field.FolderKeyPair;
            }

            set
            {
                field.FolderKeyPair = value;
            }
        }

        public void ExecuteWhere(string property, object value)
        {
            field.ExecuteWhere(property, value);
        }

        public void ExecuteWhere(System.Linq.Expressions.Expression expression)
        {
            field.ExecuteWhere(expression);
        }

        public void ExecuteWhere(XPathAxisElement element)
        {
            field.ExecuteWhere(element);
        }

        public void ClearPredicates()
        {
            field.ClearPredicates();
        }

        public IRoot Root
        {
            get
            {
                IBase baseObject = this;

                while (baseObject != null)
                {
                    baseObject = baseObject.Parent;

                    if (baseObject is IRoot)
                    {
                        return (IRoot)baseObject;
                    }
                }

                return null;
            }
        }

        [DataMember]
        public DefinitionKind Kind
        {
            get
            {
                return field.Kind;
            }
        }

        [DataMember]
        public bool HasChildren
        {
            get
            {
                return field.HasChildren;
            }
        }

        [DataMember, Include, Association("Parent_Facet", "ID", "ParentID")]
        public Facet[] Facets
        {
            get
            {
                return field.Facets;
            }
        }

        public System.Collections.Generic.IEnumerable<IElement> ChildElements
        {
            get { throw new NotImplementedException(); }
        }

        [DataMember]
        public string DefaultValue
        {
            get { throw new NotImplementedException(); }
        }

        [DataMember]
        public Modifiers Modifiers
        {
            get 
            {
                return field.Modifiers;
            }
        }

        [Exclude]
        public IProviderEntityService ProviderEntityService
        {
            get { throw new NotImplementedException(); }
        }

        IQueryable IPathQueryable.ExecuteWhere(string property, object value)
        {
            throw new NotImplementedException();
        }

        IQueryable IPathQueryable.ExecuteWhere(System.Linq.Expressions.Expression expression)
        {
            throw new NotImplementedException();
        }

        IQueryable IPathQueryable.ExecuteWhere(XPathAxisElement element)
        {
            throw new NotImplementedException();
        }
    }
}
